#include "SDL/SDL.h"
#include <windows.h>
#include "SDL/SDL_image.h"
#include "mygrafx.h"

#include "myaudio.h"

#include <cstdlib>
#include <iostream>
#include <string>

#include "enemy.h"

using namespace std;

int main( int argc, char* args[] )
{
    int speed = 500;
    //creating all players
    enemy dragon;
    enemy player1;
    enemy player2;
    enemy player3;

    //Loading bgaudio
    myaudio backgroundmusic("bgmusic.wav");
    backgroundmusic.playloop();
    myaudio action("action1.wav");
    myaudio healfail("healfail.wav");
    myaudio healsuccess("healsuccess.wav");

    //Loading all graphics
    mygrafx gamesurface;
    mygrafx sun("sun.png");
    mygrafx background("dungeon.png");
    mygrafx header("header.png");
    mygrafx hudbg("hudbg.png");
    mygrafx moon("moon.png");
    mygrafx hud("hud.png");

    mygrafx char1("Gruff2.png");
    mygrafx char2("Wiz2.png");
    mygrafx char3("Ehren2.png");

    mygrafx dragonAnim[2];
    dragonAnim[0]=("demon.png");
    dragonAnim[1]=("demon2.png");

    mygrafx player1bar("redbar.png");
    mygrafx player2bar("bluebar.png");
    mygrafx player3bar("greenbar.png");
    mygrafx dragonbar("yellowbar.png");

    mygrafx currentaction("gruffaction.png");

    //Gmame Variables
    bool quit = false;
    bool enemyturn = false;
    int char1xval=0;
    int char2xval=0;
    int char3xval=0;
    int dragonxval=0;
    int enemyanim=0;
    int player_turn=1;
    char do_action='z';
    int moonx=240;
    int moonframe=0;

    //starting x and y value for characters
    int dragony = 40;
    int char1y =160;
    int char2y=200;
    int char3y=160;

    bool alive1;
    bool alive2;
    bool alive3;
    bool alive4;

    int combo=0;
    int combo_part2=0;

    while( quit == false )
    {


            alive1 = player1.alive();
            alive2 = player2.alive();
            alive3 = player3.alive();
            alive4 = dragon.alive();

            if(!alive1 && player_turn ==1)
                player_turn=2;
            if(!alive2 && player_turn == 2)
                player_turn=3;
            if(!alive3 && player_turn == 3)
                player_turn=4;


            if(player_turn ==1)
            {
                currentaction.clean_up();
                currentaction = ("gruffaction.png");
            }
            if(player_turn ==2)
            {
                currentaction.clean_up();
                currentaction = ("wizaction.png");
            }
            if(player_turn ==3)
            {
                currentaction.clean_up();
                currentaction = ("ehrenaction.png");
            }

            if(do_action == 'a' )
            {
                if (player_turn == 1)
                {
                    int player1_attack= player1.update(player1.getHealth(),0,0);

                    if( player1_attack > 300 && alive1)
                    {
                        int damage = player1_attack - 300;
                        dragon.setDamage(damage-30);
                    }
                    else if( player1_attack > 200 && alive1)
                    {
                        int damage = player1_attack - 200;
                        dragon.setDamage(damage-30);
                    }
                    else if( player1_attack > 100 && alive1)
                    {
                        int damage = player1_attack - 100;
                        dragon.setDamage(damage-30);
                    }
                    else
                    {


                    }
                    player_turn++;
                    action.play();
                }
                else if(player_turn == 2)
                {
                    int player2_attack= player2.update(player2.getHealth(),0,0);

                    if( player2_attack > 300)
                    {
                        int damage = player2_attack - 300;
                        dragon.setDamage(damage-30);
                    }
                    else if( player2_attack > 200)
                    {
                        int damage = player2_attack - 200;
                        dragon.setDamage(damage-30);
                    }
                    else if( player2_attack > 100)
                    {
                        int damage = player2_attack - 100;
                        dragon.setDamage(damage-30);
                    }
                    else
                    {

                    }

                    player_turn++;
                    action.play();
                }
                else if(player_turn == 3)
                {
                    int player3_attack= player3.update(player3.getHealth(),0,0);

                    if( player3_attack > 300)
                    {
                        int damage = player3_attack - 300;
                        dragon.setDamage(damage-30);
                    }
                    else if( player3_attack > 200)
                    {
                        int damage = player3_attack - 200;
                        dragon.setDamage(damage-30);
                    }
                    else if( player3_attack > 100)
                    {
                        int damage = player3_attack - 100;
                        dragon.setDamage(damage-30);
                    }
                    else
                    {

                    }

                    player_turn++;
                    action.play();
                }
            }// End playaction = A
            else if(do_action == 'h' )
            {
                if (player_turn == 1)
                {
                    bool heal;
                    heal = player1.attemptHeal();

                    if(heal)
                        healsuccess.play();
                    else
                        healfail.play();

                    player_turn++;
                }
                else if(player_turn == 2)
                {
                    bool heal;
                    heal = player2.attemptHeal();

                    if(heal)
                        healsuccess.play();
                    else
                        healfail.play();

                    player_turn++;
                }
                else if(player_turn == 3)
                {
                    bool heal;
                    heal = player3.attemptHeal();

                    if(heal)
                        healsuccess.play();
                    else
                        healfail.play();

                    player_turn++;
                }//end playactin = H
            }
            else if(player_turn ==4)
            {
                    int attack= dragon.update(player1.getHealth(),player2.getHealth(),player3.getHealth());

                    if( attack > 300)
                    {
                        int damage = attack - 300;
                        player3.setDamage(damage);
                    }
                    else if( attack > 200)
                    {
                        int damage = attack - 200;
                        player2.setDamage(damage);
                    }
                    else if( attack > 100)
                    {
                        int damage = attack - 100;
                        player1.setDamage(damage);
                    }
                    else
                    {

                    }
                    dragon.playsound();
                    currentaction.clean_up();
                    currentaction = ("enemyaction.png");
                    player_turn=1;
                    enemyturn = true;
                }
                //Sleep(1500);

            //}//end of do_action

            do_action='z';



            if(!alive1)
            {
                char1.clean_up();
                char1=("blank.png");
            }
            if(!alive2)
            {
                char2.clean_up();
                char2=("blank.png");
            }
            if(!alive3)
            {
                char3.clean_up();
                char3=("blank.png");
            }
            if(!dragon.alive())
            {
                dragonAnim[0].clean_up();
                dragonAnim[0]=("blank.png");
                dragonAnim[1].clean_up();
                dragonAnim[1]=("blank.png");
            }

            ////////////END FIGHTING/////////////////////////////////////

            char1xval = (300 - player1.getHealth() );
            char2xval = (300 - player2.getHealth() );
            char3xval = (300 - player3.getHealth() );
            dragonxval = (300 - dragon.getHealth() );
            Sleep(speed);

        //Update the screen
               // draw moon
        //sun.apply_surface(0,0, gamesurface.screen);

       moon.apply_surface(moonx,40,gamesurface.screen);

        if (moonframe==0)
        {   moonx--;
            moonframe=1;
            if (moonx<-800)
            {
                moonx=750;
            }
        }
        else if(moonframe==1){moonframe=2;}
        else
            moonframe=0;

        background.apply_surface(0,0, gamesurface.screen);

        if(player_turn==1)
        {
            if ( enemyanim == 0)
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[0].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 1;
            }

            else
            {
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[1].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 0;
            }
        }
        else if (player_turn == 2)
        {
            if ( enemyanim == 0)
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[0].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 1;
            }

            else
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[1].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 0;
            }
        }
        else if (player_turn == 3)
        {
            if ( enemyanim == 0)
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[0].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 1;
            }

            else
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                dragonAnim[1].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 0;
            }
        }
        else if (player_turn == 4)
        {
            if ( enemyanim == 0)
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[0].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 1;
            }

            else
            {
                char1.apply_surface(75, char1y, gamesurface.screen);
                char2.apply_surface(150, char2y, gamesurface.screen);
                char3.apply_surface(225, char3y, gamesurface.screen);
                dragonAnim[1].apply_surface(420, dragony, gamesurface.screen);
                enemyanim = 0;
            }
        }

        hudbg.apply_surface(0,490,gamesurface.screen);
        player1bar.apply_surface(-char1xval +187, 397, gamesurface.screen);
        player2bar.apply_surface(-char2xval +187, 428, gamesurface.screen);
        player3bar.apply_surface(-char3xval +187, 458, gamesurface.screen);
        dragonbar.apply_surface(-dragonxval+187, 490, gamesurface.screen);
        hudbg.apply_surface(0,390,gamesurface.screen);


        //draw HUD
        hud.apply_surface(80, 348,gamesurface.screen);

        //draw actio
        currentaction.apply_surface(515,390, gamesurface.screen);

        SDL_Flip(gamesurface.screen);



        if(enemyturn)
        {
           Sleep(speed*3);
           enemyturn = false;
        }

        if(dragon.alive()==false)
        {
            quit = true;
        }


        SDL_Event event;
        while( SDL_PollEvent( &event ) )
        {
            switch(event.type)
            {
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_s:

                            combo = 0;
                            combo_part2 = 0;
                            break;
                        case SDLK_a:
                            do_action='a';
                            combo = 0;
                            combo_part2 = 0;
                            break;
                        case SDLK_h:
                            do_action='h';
                            combo = 0;
                            combo_part2 = 0;
                            break;
                        case SDLK_t:
                            combo = 1;
                            combo_part2 = 0;
                            break;
                        case SDLK_y:
                            if(combo == 1)
                            {
                                combo = 2;

                            }
                             combo_part2 = 0;
                            break;
                        case SDLK_u:
                            if(combo == 2)
                            {
                                action.play();
                                combo_part2 = 1;
                            }
                            else
                            {
                                combo_part2 = 0;
                            }
                            combo= 0;

                            break;
                        case SDLK_i:
                            if(combo_part2 == 1)
                            {
                                  if(speed == 500)
                                    {
                                        speed = 0;
                                    }else{
                                        speed = 500;
                                    }
                                action.play();
                                combo_part2 = 0;
                                combo = 0;
                            }
                            break;
                        default:
                            combo = 0;
                            combo_part2 = 0;
                            break;
                    }//end of switch
                    break;
                case SDL_QUIT:
                        quit = true;
                        break;
                default:
                        break;
            }
        }

        if(player1.alive() == false && player2.alive() == false && player3.alive() == false)
        {
            // this code insure that you will watch the dragon fly forever
            quit = false;
        }
    }


    return 0;
}
